package cn.edu.dlut.port.isp.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.dlut.port.isp.bean.vote.VoteBean;
import cn.edu.dlut.port.isp.repostitory.vote.VoteRepository;
import cn.edu.dlut.port.isp.service.VoteService;

@Controller
public class VoteController {
	@Autowired 
private VoteRepository voteRepository;
	@Autowired 
private VoteService voteService;
	@RequestMapping("/vote")
	public ModelAndView vote() {
		ModelAndView mav=new ModelAndView("vote");
		mav.addObject("msg","");
		return mav;
		//return "register";
	}
	@RequestMapping("/voteresult")
	public ModelAndView voteresult() {
		ModelAndView mav=new ModelAndView("voteresult");
		mav.addObject("map", voteService.getVoteResult());
		mav.addObject("map2", voteService.getVoteResult2());	
		mav.addObject("msg","");
		return mav;
		//return "register";
	}
	
	@RequestMapping("/addVote.json")
	public ModelAndView addVote(VoteBean vdata){
		ModelAndView mav=new ModelAndView("vote");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createtime = sdf.format(new Date());
		vdata.setCreatetime(createtime);
		if(voteRepository.save(vdata)!=null)
		{
			mav=new ModelAndView("voteresult");
			mav.addObject("map", voteService.getVoteResult());	
			mav.addObject("map2", voteService.getVoteResult2());	
			mav.addObject("msg","评价成功！");
		}
		else
		{
			mav.addObject("msg","评价失败！");
		}
		return mav;
	}
}
